import screenlets 
from screenlets.options import StringOption , BoolOption , IntOption , FileOption , DirectoryOption , ListOption , AccountOption , TimeOption , FontOption, ColorOption , ImageOption
from screenlets import DefaultMenuItem
import gobject
import gtk
import pango
import bandoine

class BandoineScreenlet (screenlets.Screenlet):
	"""A simple example of how to create a Screenlet"""

	__name__	= 'BandoineScreenlet'
	__version__	= '0.1'
	__author__	= 'Bandoine'
	__desc__	= __doc__ # set description to docstring of class
	
	color_text =(1.0, 1.0, 1.0, 0.7)
	font= "FreeSans"
	fontsize = 10
	starty = 0
	update_interval = 2
	variables = False

#Call super

	def __init__ (self, **keyword_args):
		screenlets.Screenlet.__init__(self, width=500, height=800, 
			uses_theme=True, **keyword_args)
		self.counter = 0
		self.timer = None
		self.title = 'Bandoine Custom Monitor'
		self.theme_name = "default"

#Option menu items:
		#self.add_options_group('Example', 'This is an example of ' +\
		#	' editable options within an OptionGroup ...')
		# add editable option to the group
		#self.add_option(StringOption('Example','test_text',			# attribute-name
		#	self.test_text,						# default-value
		#	'Test-Text', 						# widget-label
		#	'The Test-Text option for this Screenlet ...'	# description
		#	))

#Event Handler:
	def get_constant(self):
			print "CONSTANT CONSTANT CONSTANT"
			
	def get_variables(self):
		if (self.counter % 120 == 0):
			self.net_ip = bandoine.get_net_ip()
			self.open_ports = bandoine.get_open_ports()
		if (self.counter % 30 == 0):
			self.connexions = bandoine.get_tcp_connexions()
		self.variables = True
		self.lan_ip = bandoine.get_lan_ip()
		self.who = bandoine.get_who()
	
	def update (self):
		#self.get_variables()
		print "UPDATE UPDATE UPDATE"
		print "COUNTER : " + str(self.counter)
		self.get_variables()
		self.counter = self.counter + 1
		self.redraw_canvas()
		return True # keep running this event
		
	def on_map(self):
		if not self.timer:
			self.timer = gobject.timeout_add( self.update_interval*1000, self.update)
			self.counter = 0
		self.update()

	def on_unmap(self):
		if self.timer:
			gobject.source_remove(self.timer)
			self.timer = None

	def on_after_set_atribute(self,name, value):
		"""Called after setting screenlet atributes"""
		print name + ' is going to change from ' + str(value)
		pass

	def on_before_set_atribute(self,name, value):
		"""Called before setting screenlet atributes"""
		print name + ' has changed to ' + str(value)
		pass


	def on_create_drag_icon (self):
		"""Called when the screenlet's drag-icon is created. You can supply
		your own icon and mask by returning them as a 2-tuple."""
		return (None, None)

	def on_composite_changed(self):
		"""Called when composite state has changed"""
		pass

	def on_drag_begin (self, drag_context):
		"""Called when the Screenlet gets dragged."""
		pass
	
	def on_drag_enter (self, drag_context, x, y, timestamp):
		"""Called when something gets dragged into the Screenlets area."""
		pass
	
	def on_drag_leave (self, drag_context, timestamp):
		"""Called when something gets dragged out of the Screenlets area."""
		pass

	def on_drop (self, x, y, sel_data, timestamp):
		"""Called when a selection is dropped on this Screenlet."""
		return False
		
	def on_focus (self, event):
		"""Called when the Screenlet's window receives focus."""
		pass
	
	def on_hide (self):
		"""Called when the Screenlet gets hidden."""
		pass
	
	def on_init (self):
		"""Called when the Screenlet's options have been applied and the 
		screenlet finished its initialization. If you want to have your
		Screenlet do things on startup you should use this handler."""
		print 'i just got started'
		# add  menu items from xml file
		self.add_default_menuitems(DefaultMenuItem.XML)
		# add menu item
		self.add_menuitem("at_runtime", "A")
		# add default menu items
		self.add_default_menuitems()
		self.get_constant()

	def on_key_down(self, keycode, keyvalue, event):
		"""Called when a keypress-event occured in Screenlet's window."""
		key = gtk.gdk.keyval_name(event.keyval)
		
		if key == "Return" or key == "Tab":
			screenlets.show_message(self, 'This is the ' + self.__name__ +'\n' + 'It is installed in ' + self.__path__)
	
	def on_load_theme (self):
		"""Called when the theme is reloaded (after loading, before redraw)."""
		pass
	
	def on_menuitem_select (self, id):
		"""Called when a menuitem is selected."""
		if id == "at_runtime":
			screenlets.show_message(self, 'This is an example on a menu created at runtime')
		if id == "at_xml":
			screenlets.show_message(self, 'This is an example on a menu created in the menu.xml')
		pass
	
	def on_mouse_down (self, event):
		"""Called when a buttonpress-event occured in Screenlet's window. 
		Returning True causes the event to be not further propagated."""
	    
		return False
	
	def on_mouse_enter (self, event):
		"""Called when the mouse enters the Screenlet's window."""
		self.title = 'Bodji is a naab'
		pass
		
	def on_mouse_leave (self, event):
		"""Called when the mouse leaves the Screenlet's window."""
		self.title = 'Bandoine Custom Monitor'
		pass

	def on_mouse_move(self, event):
		"""Called when the mouse moves in the Screenlet's window."""
		pass

	def on_mouse_up (self, event):
		"""Called when a buttonrelease-event occured in Screenlet's window. 
		Returning True causes the event to be not further propagated."""
		return False
	
	def on_quit (self):
		"""Callback for handling destroy-event. Perform your cleanup here!"""
		#screenlets.show_question(self, 'Do you like this screenlets?')
		return True
		
	def on_realize (self):
		""""Callback for handling the realize-event."""
	
	def on_scale (self):
		"""Called when Screenlet.scale is changed."""
		pass
	
	def on_scroll_up (self):
		"""Called when mousewheel is scrolled up (button4)."""
		pass

	def on_scroll_down (self):
		"""Called when mousewheel is scrolled down (button5)."""
		pass
	
	def on_show (self):
		"""Called when the Screenlet gets shown after being hidden."""
		pass
	
	def on_switch_widget_state (self, state):
		"""Called when the Screenlet enters/leaves "Widget"-state."""
		pass
	
	def on_unfocus (self, event):
		"""Called when the Screenlet's window loses focus."""
		pass
	
	def on_draw (self, ctx):
                """In here we load the theme"""
		# if theme is loaded
		if self.variables:
			#if self.theme:
		# draw form background:
			ctx.scale(self.scale, self.scale)
			ctx.set_source_rgba(0, 0, 0.8,0.5)
			self.draw_rectangle_advanced(ctx, 0, 0, self.width-30, self.height-30, rounded_angles=(20,20,20,20), fill=True, border_size=5, border_color=(0,0,0,1), shadow_size=10, shadow_color=(0,0,0,0.5))
	
		# draw text:
			m = self.starty
			m = m + 25
			ctx.set_source_rgba( self.color_text[0], self.color_text[1], self.color_text[2],self.color_text[3])
			#Title
			self.draw_text(ctx,self.title, 0 , m, self.font , self.fontsize+2,self.width,pango.ALIGN_CENTER)
			m = m + 30
			#Lan ip
			i = 0
			for item in self.lan_ip:
				self.draw_text(ctx, item[0], -3 * (self.width/8), m, self.font , self.fontsize-1,self.width,pango.ALIGN_CENTER)
				self.draw_text(ctx, item[1], -1 * (self.width/8),m,self.font,self.fontsize-1,self.width,pango.ALIGN_CENTER)
				i = i + 1
				m = m + 20
			#Net ip
			m = m - (i * 10) - 10
			self.draw_text(ctx, 'Net ip :', 1 * (self.width/16),m,self.font,self.fontsize-1,self.width,pango.ALIGN_CENTER)
			self.draw_text(ctx, self.net_ip, 2 * (self.width/8),m,self.font,self.fontsize-1,self.width,pango.ALIGN_CENTER)
			m = m + (i * 10) + 20
			#who
			self.draw_text(ctx, 'Current shell connexions :', 0 , m, self.font , self.fontsize+1,self.width,pango.ALIGN_CENTER)
			m = m + 20
			for item in self.who:
				self.draw_text(ctx, item[0]+" "+item[1], -3 * (self.width/10),m,self.font,self.fontsize-1,self.width,pango.ALIGN_CENTER)
				self.draw_text(ctx, item[2]+":"+item[3],          0          ,m,self.font,self.fontsize-1,self.width,pango.ALIGN_CENTER)
				self.draw_text(ctx, item[4],  3 * (self.width/10),m,self.font,self.fontsize-1,self.width,pango.ALIGN_CENTER)
				m = m + 20
			#open ports
			m = m + 10
			self.draw_text(ctx, 'Open ports :', 0 , m, self.font , self.fontsize+1,self.width,pango.ALIGN_CENTER)
			m = m + 20
			for item in self.open_ports:
				self.draw_text(ctx, item[0], -2 * (self.width/10),m,self.font,self.fontsize-1,self.width,pango.ALIGN_CENTER)
				self.draw_text(ctx, item[1],          0          ,m,self.font,self.fontsize-1,self.width,pango.ALIGN_CENTER)	
				self.draw_text(ctx, item[2],  2 * (self.width/10),m,self.font,self.fontsize-1,self.width,pango.ALIGN_CENTER)
				m = m + 20
			#connexions
			m=m + 10
			self.draw_text(ctx, 'Connexions :', 0 , m, self.font , self.fontsize+1,self.width,pango.ALIGN_CENTER)
			m = m + 20
			for item in self.connexions:
				self.draw_text(ctx, item[3], -3 * (self.width/10),m,self.font,self.fontsize-1,self.width,pango.ALIGN_CENTER)	
				self.draw_text(ctx, item[4],  0 * (self.width/10),m,self.font,self.fontsize-2,self.width,pango.ALIGN_CENTER)
				self.draw_text(ctx, item[5],  3 * (self.width/10),m,self.font,self.fontsize-2,self.width,pango.ALIGN_CENTER)
				m = m + 20
			
			ctx.paint()
			
	def on_draw_shape (self, ctx):
		self.on_draw(ctx)

if __name__ == "__main__":
	# create new session
	import screenlets.session
	screenlets.session.create_session(BandoineScreenlet)
